package com.glr.service;

import com.spring.ReBeanPostProcessor;
import com.spring.ReComponent;

import java.lang.reflect.Proxy;

/**
 * Bean后处理器
 *
 * @author GLoRy
 * @date 2021/5/22 21:39
 */
@ReComponent
public class GlrBeanPostProcessor implements ReBeanPostProcessor {

    String userServiceStr = "userService";

    @Override
    public Object postProcessorBeforeInitialization(Object bean, String beanName) {
        System.out.println("初始化前");
        if (userServiceStr.equals(beanName)) {
            ((UserServiceImpl) bean).setName("glory好帅");
        }
        return bean;
    }

    @Override
    public Object postProcessorAfterInitialization(Object bean, String beanName) {
        System.out.println("初始化后");
        if (userServiceStr.equals(beanName)) {
            return Proxy.newProxyInstance(GlrBeanPostProcessor.class.getClassLoader(), bean.getClass().getInterfaces(),
                    (proxy, method, args) -> {
                        //找切点
                        System.out.println("代理逻辑");
                        return method.invoke(bean, args);
                    });
        }
        return bean;
    }
}
